$(document).ready(function() {
	/*
	 * create the loading window and set autoOpen to false
	 */
	$div = $('#panel-wait').length > 0 ? $('#panel-wait') : $('<div/>', { id: 'panel-wait' }) ;
	$div.dialog({
		autoOpen: false,	// set this to false so we can manually open it
		dialogClass: "loadingScreenWindow",
		closeOnEscape: false,
		draggable: false,
		width: 460,
		minHeight: 50,
		modal: true,
		buttons: {},
		resizable: false,
		open: function() {
			// scrollbar fix for IE
			$('body').css('overflow','hidden');
		},
		close: function() {
			// reset overflow
			$('body').css('overflow','auto');
		}
	}); // end of dialog
}); // end of document.ready()

function waitingDialog(waiting) { // I choose to allow my loading screen dialog to be customizable, you don't have to
	$("#panel-wait").html(waiting.message && '' != waiting.message ? waiting.message : 'Elaborazione in corso...');
	$("#panel-wait").dialog('option', 'title', waiting.title && '' != waiting.title ? waiting.title : 'Attendere');
	$("#panel-wait").dialog('open');
}

function closeWaitingDialog() {
	$("#panel-wait").dialog('close');
}

var ModalDialog = function() {
	settings = {
		autoOpen: true,
		modal: true,
		closeOnEscape: true,
		title: "DEA preprocessor",
	};
	
	return { open: open, info: info, alert: alert };
	
	function open( config ) {
		var $div = $('#modal-dialog');
		if ($div.length == 0) {
			$div = $('<div/>', { id: 'modal-dialog' }).appendTo('body');
			$p = $('<p/>', { class: 'modal-dialog-content' }).appendTo($div);
			$('<span/>', { class: 'ui-icon modal-icon'}).appendTo($p);
			$('<span/>').appendTo($p);
		}
		var defaultButtons = {
			Ok: function() { $div.dialog('close'); }
		};
		if (typeof config.message !== 'undefined') { $('span', $div).eq(1).text(config.message); }
		if (typeof config.type === 'undefined') { config.type = 'info'; }
		$('span', $div).eq(0).removeClass().addClass('ui-icon').addClass('modal-icon');
		
		switch (config.type) {
		case 'confirm':
			$('span', $div).eq(0).addClass('ui-icon-notice');
			defaultButtons = {
				Ok: (typeof config.callback !== 'undefined') ? config.callback() : defaultButtons.Ok,
				Annulla: function() { $div.dialog('close'); }
			};
			break;
		case 'warn':
		case 'warning':
		case 'alert':
			$('span', $div).eq(0).addClass('ui-icon-alert');
			break;
		case 'info':
		default:
			$('span', $div).eq(0).addClass('ui-icon-info');
			break;
		}
		
		options = $.extend({}, settings, { buttons: defaultButtons }/*, config*/);
		//console.log(options);
		$div.dialog(options);
	}
	
	function info (message) {
		open ({ message: message, type: 'info'}); 
	}
	function alert (message) {
		open ({ message: message, type: 'alert'}); 
	}

}()

